package com.hmall.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilter;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * 局部过滤器;需要在配置之后才能生效
 */
@Component
public class PrintAnyGatewayFilterFactory extends AbstractGatewayFilterFactory<Object> {
    @Override
    public GatewayFilter apply(Object config) {
        return new GatewayFilter() {
            @Override
            public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {

                System.out.println("执行了局部过滤器 PrintAnyGatewayFilterFactory ：" + exchange.getRequest().getPath());
                // 放行
                return chain.filter(exchange);
                //设置响应状态码为；没有授权
                /*exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
                //响应结束
                return exchange.getResponse().setComplete();*/
            }
        };
    }
}
